﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace PHTShop
{
    public partial class WebForm5 : basePageUser
    {
        private DataContextDataContext dc = new DataContextDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!IsPostBack)
            {
                if (Session["Account"] != null)
                {
                    Response.Redirect("cart.aspx");
                }
                else
                {
                     switch (Session["languages"].ToString().ToLower())
                    {
                        case "france":
                            Page.Title = "Mon compte";
                            break;
                        case "germany":
                            Page.Title = "Mein Konto";
                            break;
                        default:
                            Page.Title = "My account";
                            break;
                    }
                }
            }
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            Setting th = this.dc.Settings.Single(t => t.type.Equals("theme") && Convert.ToInt32(t.value) == 1);
            Page.Theme = th.name.ToString();
        }

        protected void login_click(object sender, EventArgs e)
        {
            this.Labelusername.Text = "";
            this.Labelpassword.Text = "";
            if (this.check(this.username.Value) == true)
            {
                this.Labelusername.Text = "not null";
            }
            else if (this.check(this.password.Value) == true)
            {
                this.Labelpassword.Text = "not null";
            }
            else
            {
                try
                {
                    string username = this.username.Value;
                    string password = FormsAuthentication.HashPasswordForStoringInConfigFile(this.password.Value, "sha1");
                    Account ac = this.dc.Accounts.Single(t => t.username.Equals(username) && t.password.Equals(password));

                    Session["Account"] = ac;

                    Response.Redirect("Default.aspx");
                }
                catch
                {
                    this.Labelfalse.Text = "Username or Password false !";
                }
            }
        }

        private bool check(string str)
        {
            return String.IsNullOrEmpty(str.Trim());
        }
    }
}